#!/bin/bash
########################################################
#                                                      # 
#   checkStatus()                                      #
#   Usage:                                             #
#                                                      #
########################################################

APID=$(ps -A -o pid,cmd | awk '$2=="SCREEN" && $4=="AuthServer" {print $1}')
WPID=$(ps -A -o pid,cmd | awk '$2=="SCREEN" && $4=="WorldServer" {print $1}')
TPID=$(ps -A -o pid,cmd | awk '$2=="SCREEN" && $4=="TestServer" {print $1}')

counter=0

observermode=0
if [ $1 == 'observermode' ]; then
	observermode=1
	shift
fi

while [ $# -ne 0 ]; do

case $1 in
	authserver)
		DATE=$(date)
		
		if [ -n "$APID" ]; then

			if [ $observermode -eq 1 ]; then
				let counter+=1
			else
				echo "AuthServer is running! ($DATE) PID: $APID"
				let counter+=1
			fi

		else

			if [ $observermode -eq 1 ]; then
				error+=1
			else
				echo "AuthServer is down! ($DATE)"	
			fi
				
		fi
			
	;;

	worldserver)
		DATE=$(date)

		if [ -n "$WPID" ]; then
		
			if [ $observermode -eq 1 ]; then
				let counter+=2
			else
				echo "WorldServer is running! ($DATE) PID: $WPID"
				let counter+=2
			fi	

		else
			if [ $observermode -eq 1 ]; then
				error+=1
			else
				echo "WorldServer is down! ($DATE)"	
			fi

		fi

	;;

	testserver)
		DATE=$(date)

		if [ -n "$TPID" ]; then

			if [ $observermode -eq 1 ]; then
				let counter+=4
			else
				echo "TestServer is running! ($DATE) PID: $TPID"
				let counter+=4
			fi

		else

			if [ $observermode -eq 1 ]; then
				error+=1
			else
				echo "TestServer is down! ($DATE)"
			fi
		
		fi

	;;	

esac

shift

done

exit $counter
